/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Forms;

import Controls.Button;
import Controls.StringInput;
import Controls.Text;
import Core.ALIGN;
import Core.Control;
import Core.ControlImage;
import Core.Form;
import Core.GM;
import Core.GUICanvas;
import Core.Key;
import Core.LayoutManager;
import Core.Spacer;

/**
 *
 * @author Александр
 */
public class AddVar extends Form
{
	public static int	ABOVE = 0,
						BELOW = 1,
						TOP = 2,
						BOTTOM = 3;

	public int align = -1;
	public String varName = "";

	private Button above, below, top, bottom;
	private StringInput stringInput;

	private static ControlImage i_top = new ControlImage("/add_top.png", ALIGN.RIGHT),
								i_bottom = new ControlImage("/add_bottom.png", ALIGN.RIGHT),
								i_after = new ControlImage("/add_after.png", ALIGN.RIGHT),
								i_before = new ControlImage("/add_before.png", ALIGN.RIGHT);

	public AddVar()
	{
		super(Form.HEAD|Form.NO_FS|Form.NO_BG|Form.CENTRALISE_FROM_BG);

		body.h_border = 3;
		body.addControl(new Text("Name:"));
		stringInput = new StringInput(body.getAvailableW(), "");
		body.addControl(stringInput);

		LayoutManager raw1 = new LayoutManager(body.getAvailableW(), 0);
		raw1.type = LayoutManager.HORIZONTAL;
		raw1.spacing = body.spacing;
		raw1.allowToEnlarge = true;

		above = new Button("", i_before.getW()+6, i_before.getH()+4);
		below = new Button("", i_after.getW()+6, i_after.getH()+4);
		top =  new Button("",  i_top.getW()+6, i_top.getH()+4);
		bottom =  new Button("", i_bottom.getW()+6, i_bottom.getH()+4);

		above.addImage(i_before);
		below.addImage(i_after);
		top.addImage(i_top);
		bottom.addImage(i_bottom);
		
		body.setHotControl(above, 1);
		body.setHotControl(below, 2);
		body.setHotControl(top, 3);
		body.setHotControl(bottom, 4);

		raw1.addControl(above);
		//raw1.addControl(new Spacer());
		raw1.addControl(below);
		//raw1.addControl(new Spacer());
		raw1.addControl(top);
		//raw1.addControl(new Spacer());
		raw1.addControl(bottom);

		body.addControl(raw1);

		setWindowText(GM.PANEL_RIGHTSOFT, "Cancel");
		setWindowText(GM.FORM_CAPTION, "Create var");

		form.bg = GUICanvas.pool.CreateImage("/black.cfg", form.getW(), head.getH() + body.getMinH()+form.spacing+4, Form.StandartBGColor, false);

		body.select(Control.UNDER);
	}

	void tryToClose()
	{
		if(stringInput.string.length() != 0)
		{
			varName = stringInput.string;
			close();
		}
	}

	public void onEvent(int event, Object param)
	{
		super.onEvent(event, param);

		if(event == GM.CONTROL_RELEASED)
		{
			if(param == top)
			{
				align = TOP;
				tryToClose();
			}
			else if(param == bottom)
			{
				align = BOTTOM;
				tryToClose();
			}
			else if(param == below)
			{
				align = BELOW;
				tryToClose();
			}
			else if(param == above)
			{
				align = ABOVE;
				tryToClose();
			}
		}
	}

	public void processKey(int key, int state)
	{
		super.processKey(key, state);
		
		if(key == Key.STAR_KEY && state == Key.RELEASED)
		{
			align = -1;
			close();
		}
	}





}
